﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace SmartSpace.Core
{
    public static class EntityRepository
    {
        public static void Save<TEntity>(TEntity entity, DbContext context) where TEntity : class, IGuidKeyEntity
        {
            if (context.Set<TEntity>().Where(p => p.Id == entity.Id)
                .Select(p => p.Id).SingleOrDefault() == default(Guid))
            {
                context.Set<TEntity>().Add(entity);
            }
            else
            {
                context.Entry(entity).State = EntityState.Modified;
            }
        }

        public static bool Delete(IGuidKeyEntity entity, DbContext context)
        {
            if (context.Set<IGuidKeyEntity>().Where(p => p.Id == entity.Id)
                .Select(p => p.Id).SingleOrDefault() == default(Guid))
            {
                return true;
            }
            else
            {
                context.Entry(entity).State = EntityState.Deleted;
                return true;
            }
        }

        public static bool Delete(Guid entityId, DbContext context)
        {
            IGuidKeyEntity entity = context.Set<IGuidKeyEntity>().Where(p => p.Id == entityId).SingleOrDefault();
            if (entity == null)
            {
                return true;
            }
            else
            {
                context.Entry(entity).State = EntityState.Deleted;
            }

            if (context.SaveChanges() > 0)
            {
                return true;
            }
            return false;
        }

        public static IGuidKeyEntity Get(Guid entityId, DbContext context)
        {
            return context.Set<IGuidKeyEntity>().Where(p => p.Id == entityId).Single();
        }

        public static bool Commit(DbContext context)
        {
            try
            {
                if (context.SaveChanges() > 0)
                {
                    return true;
                }
            }
            catch
            {
            }
            return false;
        }

        /// <summary>
        /// 删除继承了接口ICodeEntity的实体对象，
        /// 在这个操作之后需要调用Commit方法
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static bool SaveCodeEntity<TEntity>(TEntity entity, DbContext context) where TEntity : class, ICodeEntity
        {
            if (context.Set<TEntity>().Where(p => p.Id == entity.Id && p.Code == entity.Code)
                .Select(p => p.Id).SingleOrDefault() != default(Guid))
            {
                context.Entry(entity).State = EntityState.Modified;
            }
            else if (context.Set<TEntity>().Where(p=> p.Code == entity.Code)
                .Select(p => p.Id).SingleOrDefault() != default(Guid))
            {
                return false;
            }
            else
            {
                context.Set<TEntity>().Add(entity);
            }
            return true;
        }

        public static bool DeleteCodeEntity<TEntity>(TEntity entity, DbContext context) where TEntity : class, ICodeEntity
        {
            if (context.Set<TEntity>().Where(p => p.Id == entity.Id && p.Code == entity.Code)
                .Select(p => p.Id).SingleOrDefault() == default(Guid))
            {
                return true;
            }
            else
            {
                context.Entry(entity).State = EntityState.Deleted;
                return true;
            }
        }

        public static TEntity GetCodeEntity<TEntity>(string code, DbContext context) where TEntity : class, ICodeEntity
        {
            return context.Set<TEntity>().Where(p => p.Code == code).SingleOrDefault();
        }

        public static TEntity GetCodeEntity<TEntity>(Guid id, DbContext context) where TEntity : class, ICodeEntity
        {
            return context.Set<TEntity>().Where(p => p.Id == id).SingleOrDefault();
        }

        public static bool DeleteCodeEntity<TEntity>(string code, DbContext context) where TEntity : class, ICodeEntity
        {
            TEntity entity = context.Set<TEntity>().Where(p => p.Code == code).SingleOrDefault();
            if (entity == null)
            {
                return true;
            }
            else
            {
                context.Entry(entity).State = EntityState.Deleted;
            }
            return true;
        }

        public static bool DeleteCodeEntity<TEntity>(Guid id, DbContext context) where TEntity : class, ICodeEntity
        {
            TEntity entity = context.Set<TEntity>().Where(p => p.Id == id).SingleOrDefault();
            if (entity == null)
            {
                return true;
            }
            else
            {
                context.Entry(entity).State = EntityState.Deleted;
            }
            return true;
        }
    }
}
